// pages/author/author.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
const app = getApp();
const appid = 'wxe5d25f7c7ea9538c';
const secret = '74512693df3306dbe96b499af4429d9e';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hasread: true, //是否已读用户协议
    },
    //阅读、取消阅读用户协议
    readProtocol_confirm: function(e) {
        var hasread = this.data.hasread;
        this.setData({
            hasread: !hasread
        })
    },
    //返回首页
    goindex: function(e) {
        wx.switchTab({
            url: '../index/index'
        })
    },
    goUserProtocol: function(e) {
        wx.navigateTo({
            url: '../userProtocol/userProtocol'
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },
    //获取手机号函数
    getPhoneNumber: function(e) {

    },
    //点击授权按钮
    ongetUserInfo: function(e) {
        console.log(e)

        var that = this;
        var hasread = this.data.hasread;
        if (!hasread) {
            wx.showToast({
                title: '请阅读并同意用户协议',
                icon: 'none',
                mask: true,
            })
            return false;
        }
        wx.getSetting({
            success: function(res) {
				console.log(res)
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: function(res) {
                            var userInfo = res;
                            console.log(res)
                            wx.login({
                                success: function(res) {
                                    var jsCode = res.code;
                                    app.aldpush.pushuserinfo(userInfo, jsCode);
                                    if (res.code) {
                                        var code = res.code;
                                        wx.request({
                                            url: url + 'User/login',
                                            data: {
                                                code: code,
                                                APP_ID: APP_ID,
                                                sign: md5([
                                                    "code" + code,
                                                    "APP_ID" + APP_ID
                                                ])
                                            },
                                            header: {},
                                            method: 'post',
                                            dataType: 'json',
                                            responseType: 'text',
                                            success: function(res) {
                                                var session_str = res.data.data.session_str;
                                                if (!res.data.data.is_info) {
                                                    wx.redirectTo({
                                                        url: '../getphone/getphone?str=' + session_str,
                                                    })
                                                } else {
                                                    wx.setStorageSync('session_str', session_str);
                                                    wx.navigateBack({
                                                        delta: -1,
                                                    })
                                                }
                                            },
                                            fail: function(res) {

                                            }
                                        });
                                    }
                                },
                                fail: function(res) {},
                                complete: function(res) {},
                            })
                        }
                    })


                }else{
					console.log('未授权')
					
				}
            }
        })
        return false;
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        wx.getSetting({
            success: function(res) {
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: function(res) {
                            var userInfo = res;
                            console.log(res)
                            wx.login({
                                success: function(res) {
                                    var jsCode = res.code;
                                    app.aldpush.pushuserinfo(userInfo, jsCode);
                                }
                            })
                        }
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})